import kshDregsDataApi from '../../api/kshDregsData'

export const ZtMixins = {
  components: {
  },
  data() {
    return {
      // 环形图-事件动态 Loding
      sjdtLoading: false,
      // 环形图-事件动态数据
      sjdtData: null,
      // 基础信息
      ztBaseInfoLoading: false,
      ztBaseInfoData: [
        {label: '工地数量', value: '-', unit: '个'},
        {label: '车辆数量', value: '-', unit: '辆'},
        {label: '消纳场数量', value: '-', unit: '个'},
        {label: '运输单位数量', value: '-', unit: '个'}
      ],
      // 报警
      ztBjLoading: false,
      ztBjData: null,
      // 消纳场
      ztXncLoading: false,
      ztXncData: {total: '-', data: []},
      // 渣土车基础信息
      ztZtcBaseInfoLoading: false,
      ztZtcBaseInfoData: [
        {label: '行驶', value: '-', unit: '辆'},
        {label: '静止', value: '-', unit: '辆'},
        {label: '离线', value: '-', unit: '辆'},
        {label: '异常', value: '-', unit: '辆'}
      ],
      // 工地
      ztGdLoading: false,
      ztGdData: {total: '-', data: []},
      ztCarPoints: [],
      ztTimer: null
    }
  },
  methods: {
    // 进入页面时执行
    openZtPage() {
      this.handleGetBasicInformation()
      this.handleGetEventDynamicsByDayStatistics()
      this.handleGetEventDynamicsStatistics()
      this.handleGetDregsCarMsg()
      this.handleGetDSoilFieldInputStatistics()
      this.handleGetSiteOutPutStatistics()
      this.updateZtData()
      setTimeout(() => {
        this.handleGetCarByStatus(2)
      }, 500)
    },
    // 获取环形图数据
    handleGetEventDynamicsStatistics() {
      const {startTime, endTime} = this.getCurrentDate('last7')
      // this.sjdtLoading = true
      kshDregsDataApi.getEventDynamicsStatistics({startTime: startTime, endTime: endTime})
        .then(res => {
          const data = res.data
          this.sjdtData = JSON.parse(JSON.stringify(data).replace(/typeName/g, 'name').replace(/cou/g, 'value'))
          // this.sjdtLoading = false
        })
        .catch(() => {
          // this.sjdtLoading = false
        })
    },
    // 获取基础信息
    handleGetBasicInformation() {
      // this.ztBaseInfoLoading = true
      kshDregsDataApi.getBasicInformation()
        .then(res => {
          const data = res.data
          this.ztBaseInfoData = [
            {label: '工地数量', value: data.workSiteCount || '0', unit: '个'},
            {label: '车辆数量', value: data.carCount || '0', unit: '辆'},
            {label: '消纳场数量', value: data.earthSitesCount || '0', unit: '个'},
            {label: '运输单位数量', value: data.companyCount || '0', unit: '个'}
          ]
          // this.ztBaseInfoLoading = false
        })
        .catch(() => {
          // this.ztBaseInfoLoading = false
        })
    },
    // 获取报警信息
    handleGetEventDynamicsByDayStatistics() {
      const {startTime, endTime} = this.getCurrentDate('last7')
      // this.ztBjLoading = true
      kshDregsDataApi.selectEventDynamicsByDayStatistics({startTime: startTime, endTime: endTime})
        .then(res => {
          const data = res.data
          this.ztBjData = {name: [], value: []}
          data.forEach(item => {
            this.ztBjData.name.push(item.dateName)
            this.ztBjData.value.push(item.cou)
          })
          // this.ztBjLoading = false
        })
        .catch(() => {
          // this.ztBjLoading = false
        })
    },
    // 获取消纳场数据
    handleGetDSoilFieldInputStatistics() {
      // this.ztXncLoading = true
      kshDregsDataApi.getDSoilFieldInputStatistics()
        .then(res => {
          this.ztXncData = res.data
          this.ztXncData.max = 0
          this.ztXncData.data.forEach(item => {
            if (item.dayAbility > this.ztXncData.max) {
              this.ztXncData.max = item.dayAbility
            }
          })
          // this.ztXncLoading = false
        })
        .catch(() => {
          // this.ztXncLoading = false
        })
    },
    // 获取工地输出数据
    handleGetSiteOutPutStatistics() {
      // this.ztGdLoading = true
      kshDregsDataApi.getSiteOutPutStatistics()
        .then(res => {
          this.ztGdData = res.data
          this.ztGdData.max = 0
          this.ztGdData.data.forEach(item => {
            if (item.sumCount > this.ztGdData.max) {
              this.ztGdData.max = item.sumCount
            }
          })
          // this.ztGdLoading = false
        })
        .catch(() => {
          // this.ztGdLoading = false
        })
    },
    // 获取车辆基础信息数据
    handleGetDregsCarMsg() {
      // this.ztZtcBaseInfoLoading = true
      kshDregsDataApi.getDregsCarMsg()
        .then(res => {
          const data = res.data
          this.ztZtcBaseInfoData = [
            {label: '行驶', value: data.runningCount || '0', unit: '辆', code: 2},
            {label: '静止', value: data.stopCount || '0', unit: '辆', code: 3},
            {label: '离线', value: data.unlineCount || '0', unit: '辆', code: 1},
            {label: '异常', value: data.errorCount || '0', unit: '辆', code: 0}
          ]
          // this.ztZtcBaseInfoLoading = false
        })
        .catch(() => {
          // this.ztZtcBaseInfoLoading = false
        })
    },
    // 获取渣土车数据
    handleGetCarByStatus(status = 2) {
      // this.ztZtcBaseInfoLoading = true
      this.ztCarInfoVisible = false
      kshDregsDataApi.getCarByStatus(status)
        .then(res => {
          this.ztCarPoints = res.data
          this.$refs.map.clearMarkers()
          // this.$refs.map.addCircleMarkers(this.ztCarPoints, 'ztc')
          const img = require('../../assets/image/yx/bus.png')
          const selectedImg = require('../../assets/image/yx/bus-selected.png')
          this.$refs.map.addIconMarkers2(this.ztCarPoints, img, selectedImg, 'yx_bus_info', {size: [36, 36], offset: [-14, -13]})
          // this.ztZtcBaseInfoLoading = false
        })
        .catch(() => {
          // this.ztZtcBaseInfoLoading = false
        })
    },
    updateZtData() {
      this.ztTimer = setInterval(() => {
        if (this.headerTabCurrent.code === 'zt') {
          this.handleGetBasicInformation()
          this.handleGetDregsCarMsg()
        } else {
          clearInterval(this.ztTimer)
        }
      }, 180000)
    }
  }
}
